package com.example.aircraft.panel;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;


import com.example.aircraft.MyApplication;
import com.example.aircraft.R;
import com.example.aircraft.database.MySqlManager;
import com.example.aircraft.database.MySqliteHelper;
import com.example.aircraft.database.Rank_Data;
import com.example.aircraft.database.SQLiteInterface;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;


public class RankPanel extends Activity {
    private static final String TAG = "change";
    public MySqliteHelper helper;
    private ArrayList<Rank_Data> ListofRank;
    private int targetD;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        helper = MySqlManager.getInstance(MyApplication.getContext());
        SQLiteDatabase db = helper.getReadableDatabase();
        String selectsql = "select * from "+ SQLiteInterface.TABLE1_NAME;

        ListofRank = MySqlManager.cursor1ToList(MySqlManager.selectSQL(db, selectsql, null));
        Collections.sort(ListofRank);
        db.close();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rank_panel);
        setupViews();
    }

    public void setupViews(){
        MyAdapter myAdapter = new MyAdapter();
        ListView listView = findViewById(R.id.gridViewRank);
        listView.setAdapter(myAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                targetD = i;
                toDelete();
            }
        });
    }

////
    public void updateView(){
        SQLiteDatabase db = helper.getReadableDatabase();
        String selectsql = "select * from "+ SQLiteInterface.TABLE1_NAME;
        ListofRank = MySqlManager.cursor1ToList(MySqlManager.selectSQL(db, selectsql, null));
        setupViews();
        db.close();
    }
////

    public void toDelete() {
        android.app.AlertDialog.Builder alertdialogbuilder = new android.app.AlertDialog.Builder(this);
        alertdialogbuilder.setMessage("确定要删除该条记录吗");
        alertdialogbuilder.setPositiveButton("确定", click1);
        alertdialogbuilder.setNegativeButton("取消", click2);
        AlertDialog alertDialog = alertdialogbuilder.create();
        alertDialog.show();
    }

    private DialogInterface.OnClickListener click1 = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            MySqlManager.deleteData(String.valueOf(ListofRank.get(targetD).getID()));
            updateView();
            dialog.cancel();
        }
    };

    private DialogInterface.OnClickListener click2 = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    };


    public void playAgain(View view) {
        Log.d(TAG, "Play it Again");
        Intent intent = new Intent(this,ChoosePattern.class);
        startActivity(intent);
        finish();
    }

    public void backToMenu(View view) {
        Log.d(TAG, "Go Back to Menu: ");
        Intent intent = new Intent(this, MenuActivity.class);
        startActivity(intent);
        finish();
    }

    static class ViewHolder {
        TextView rank;
        TextView name;
        TextView score1;
        TextView level;
        TextView time;
    }

    private class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return ListofRank.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            RankPanel.ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(RankPanel.this, R.layout.ranklist, null);
                holder = new RankPanel.ViewHolder();
                holder.rank = convertView.findViewById(R.id.rankofRank);
                holder.name = convertView.findViewById(R.id.nameofRank);
                holder.score1 = convertView.findViewById(R.id.scoreofRank);
                holder.level = convertView.findViewById(R.id.levelofRank);
                holder.time = convertView.findViewById(R.id.timeofRank);
                convertView.setTag(holder);
            } else {
                holder = (RankPanel.ViewHolder) convertView.getTag();
            }
            holder.rank.setText(String.valueOf(position+1));//position
            holder.name.setText(ListofRank.get(position).getUSER_NAME());//ListofRank.get(position).getUSER_NAME()
            holder.score1.setText(String.valueOf(ListofRank.get(position).getSCORE()));//ListofRank.get(position).getSCORE()
            holder.level.setText(ListofRank.get(position).getLevel());//ListofRank.get(position).getLevel()
            holder.time.setText(ListofRank.get(position).getTIME());//ListofRank.get(position).getTIME()
            holder.rank.setTextColor(Color.WHITE);
            holder.name.setTextColor(Color.WHITE);
            holder.score1.setTextColor(Color.WHITE);
            holder.level.setTextColor(Color.WHITE);
            holder.time.setTextColor(Color.WHITE);

            return convertView;
        }

        }
    }

